package com.colortea.action.frontstage.membercenter;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.InterceptorRefs;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;

import com.colortea.action.backstage.base.BaseTAction;
import com.colortea.action.backstage.base.DataCollectionTAction;
import com.colortea.action.backstage.utils.BaseTools;
import com.colortea.action.backstage.utils.statickey.StaticKey;
import com.colortea.entity.DeliverAddressT;
import com.colortea.entity.MemberT;
import com.colortea.service.DeliverAddressTService;
import com.colortea.service.impl.Serial;
import com.opensymphony.xwork2.ActionContext;

@ParentPackage("colortea")
@Namespace("/mcenter")
@InterceptorRefs({ @InterceptorRef("defaultStack") })
public class DeliverAddressAction extends BaseTAction {

	/**
	 * 
	 */

	private static final long serialVersionUID = 7756320329883002073L;
	@Resource
	private DataCollectionTAction dataCollectionTAction;
	@Resource
	private DeliverAddressTService deliverAddressTService;

	@Action(value = "savedeliveraddress", results = { @Result(name = "json", type = "json") })
	public String saveDeliverAddress() {
		MemberT member=(MemberT)ActionContext.getContext().getSession().get(StaticKey.MEMBER_SESSION_KEY);
		if(member!=null){
			this.setSlogin(true);
			DeliverAddressT d=new DeliverAddressT();
			d.setAddressid(this.getSerial().Serialid(Serial.DELIVERADDRESS));
			d.setMemberid(member.getId());
			d.setShippingusername(this.getShippingusername().trim());
			d.setCountry(this.getCountry().trim());
			d.setProvince(this.getProvince().trim());
			d.setCity(this.getCity().trim());
			d.setDistrict(this.getDistrict().trim());
			d.setStreet(this.getStreet().trim());
			d.setPostcode(this.getPostcode().trim());
			d.setTelno(StaticKey.EMPTY);
			d.setMobile(this.getMobile());
			d.setEmail(StaticKey.EMPTY);
			d.setAddresstag(this.getAddresstag());
			d.setCreatetime(BaseTools.getSystemTime());
			d.setState(StaticKey.ZERO);//0非默认，1默认
			this.deliverAddressTService.save(d);
			this.setSucflag(true);
			return JSON;
		}
		this.setSlogin(false);		
		return JSON;
	}
	/**
	 * 进入收货地址界面
	 * @return
	 */
	@Action(value = "goaddressmanage", results = { 
			@Result(name = "success",type="freemarker",location = "/WEB-INF/theme/default/shop/addressmanage.ftl"),
			@Result(name = "gologin",type="freemarker",location = "/WEB-INF/theme/default/shop/login.ftl"),
	})
	public String goaddressmanage(){
		dataCollectionTAction.putBaseInfoToContext();
		MemberT memberT=(MemberT) ActionContext.getContext().getSession().get(StaticKey.MEMBER_SESSION_KEY);
		if(memberT!=null){
			Criterion criterion = Restrictions.and(Restrictions.eq("memberid",memberT.getId()));
			List<DeliverAddressT> list = deliverAddressTService.findByCriteria(DeliverAddressT.class, criterion);
			if(list.size()>0){
				ActionContext.getContext().getSession().put("deliveraddressList", list);
			}			
			return SUCCESS;
		}
		return "gologin";		
	}
	/**
	 * 根据id删除收货地址
	 * @param id
	 * @return
	 */
	@Action(value = "deladdressById", results = { @Result(name = "json", type = "json") })
	public String deladdressById(){
		dataCollectionTAction.putBaseInfoToContext();
		MemberT memberT=(MemberT) ActionContext.getContext().getSession().get(StaticKey.MEMBER_SESSION_KEY);
		if(memberT!=null){
			this.setSlogin(true);
			if(StringUtils.isNotBlank(this.getId())){
				DeliverAddressT addressT = deliverAddressTService.findByPK(DeliverAddressT.class, this.getId());
				if(addressT != null){
					deliverAddressTService.delete(addressT);
					this.setSucflag(true);		
					return "json";
				}
				this.setSucflag(false);
				return "json";
			}			
			return "json";
		}
		this.setSlogin(false);
		return "json";
	}
	
	/**
	 * 根据id修改收货地址
	 */
	@Action(value = "updateAddressById", results = { @Result(name = "json", type = "json") })
	public String updateAddressById() {
		dataCollectionTAction.putBaseInfoToContext();
		MemberT member=(MemberT)ActionContext.getContext().getSession().get(StaticKey.MEMBER_SESSION_KEY);
		if(member!=null){
			this.setSlogin(true);
			if(StringUtils.isNotBlank(this.getId())){
				DeliverAddressT addressT = deliverAddressTService.findByPK(DeliverAddressT.class, this.getId());
				if(addressT != null){
					addressT.setShippingusername(this.getShippingusername().trim());
					addressT.setCountry(this.getCountry().trim());
					addressT.setProvince(this.getProvince().trim());
					addressT.setCity(this.getCity().trim());
					addressT.setDistrict(this.getDistrict().trim());
					addressT.setStreet(this.getStreet().trim());
					addressT.setPostcode(this.getPostcode().trim());
					addressT.setMobile(this.getMobile());
					addressT.setAddresstag(this.getAddresstag());
					this.deliverAddressTService.update(addressT);
					this.setSucflag(true);
					return "json";
				}
			}			
			return "json";
		}
		this.setSlogin(false);
		return "json";
	}
	
	
	
	private String id;
	private String city;
	private String country;
	private String district;
	private String mobile;
	private String postcode;
	private String street;
	private String shippingusername;
	private String province;
	private String addresstag;
	private boolean sucflag;
	private boolean slogin;
	
	public String getAddresstag() {
		return addresstag;
	}

	public void setAddresstag(String addresstag) {
		this.addresstag = addresstag;
	}

	public boolean isSucflag() {
		return sucflag;
	}

	public void setSucflag(boolean sucflag) {
		this.sucflag = sucflag;
	}

	public String getProvince() {
		return province;
	}

	public void setProvince(String province) {
		this.province = province;
	}

	public String getCity() {
		return city;
	}

	public void setCity(String city) {
		this.city = city;
	}

	public String getCountry() {
		return country;
	}

	public void setCountry(String country) {
		this.country = country;
	}

	public String getDistrict() {
		return district;
	}

	public void setDistrict(String district) {
		this.district = district;
	}

	public String getMobile() {
		return mobile;
	}

	public void setMobile(String mobile) {
		this.mobile = mobile;
	}

	public String getPostcode() {
		return postcode;
	}

	public void setPostcode(String postcode) {
		this.postcode = postcode;
	}

	public String getStreet() {
		return street;
	}

	public void setStreet(String street) {
		this.street = street;
	}

	public String getShippingusername() {
		return shippingusername;
	}

	public void setShippingusername(String shippingusername) {
		this.shippingusername = shippingusername;
	}
	public boolean isSlogin() {
		return slogin;
	}
	public void setSlogin(boolean slogin) {
		this.slogin = slogin;
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	
}
